<template>
  <el-dialog
    title="新規"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form size="small" :model="form" ref="form">
      <el-form-item
        label="タイトル"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: ' ' }]"
        prop="title"
      >
        <el-input
          v-model="form.title"
          autocomplete="off"
          placeholder="タイトル"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="銀行名"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: ' ' }]"
      >
        <el-select v-model="form.bank_id" placeholder="選択してください" @change="getBranchList">
          <el-option
            v-for="item in bankList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="支店名"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: ' ' }]"
      >
        <el-select v-model="form.branch_id" placeholder="選択してください">
          <el-option
            v-for="item in branchList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="口座種別" :label-width="formLabelWidth">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">普通</el-radio>
          <el-radio :label="2">当座</el-radio>
          <el-radio :label="3">その他</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="口座番号"
        :label-width="formLabelWidth"
        prop="account"
      >
        <el-input
          v-model="form.account"
          autocomplete="off"
          placeholder="口座番号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="口座名"
        :label-width="formLabelWidth"
        prop="account_name"
      >
        <el-input
          v-model="form.account_name"
          autocomplete="off"
          placeholder="口座名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="振込先"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: ' ' }]"
        prop="address"
      >
        <el-input
          v-model="form.address"
          autocomplete="off"
          placeholder="振込先"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="備考"
        :label-width="formLabelWidth"
        prop="meno"
      >
        <el-input
          v-model="form.meno"
          autocomplete="off"
          placeholder="備考"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">キャンセル</el-button>
        <el-button type="primary" @click="addClick()" :loading="loading"
          >確定</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
import bankApi from "@/api/bank.js";
import branchApi from "@/api/branch.js";
import bankaccountApi from "@/api/bankaccount.js";
export default {
  data() {
    return {
      form: {
        bank_id: '',
        type: 1,
      },
      categoryList: [],
      sort: "100",
      radio: "1",
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      bankList: [],
      branchList: [],

    };
  },
  props: ["open_add"],
  created() {
    this.dialogVisible = this.open_add;
    this.getBankList();
  },
  methods: {
    /*获取列表*/
    getBankList() {
      let self = this;
      let Params = {};
      bankApi
        .lists(Params, true)
        .then((data) => {
          self.loading = false;
          self.bankList = data.data.list;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*获取列表*/
    getBranchList() {
      let self = this;
      let Params = {
        bank_id: this.form.bank_id
      };
      branchApi
        .getLists(Params, true)
        .then((data) => {
          self.loading = false;
          self.branchList = data.data.list;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*添加插件*/
    addClick() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate((valid) => {
        if (valid) {
          self.loading = true;
          bankaccountApi
            .adds(params, true)
            .then((res) => {
              if (res.code == 1) {
                self.loading = false;
                ElMessage({
                  message: "追加成功",
                  type: "success",
                });
                self.dialogFormVisible(true);
              } else {
                self.loading = false;
              }
            })
            .catch((error) => {});
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false,
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false,
        });
      }
    },
  },
};
</script>

<style></style>
